import {
  createRouter,
  createWebHashHistory,
  RouteRecordRaw,
  RouterView
} from "vue-router";
import LocalCache from "@/utils/cache";
const routes: RouteRecordRaw[] = [
  {
    path: "/",
    redirect: "/main/index/index"
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/login.vue")
  },
  {
    path: "/main",
    name: "main",
    component: () => import("@/views/main/main.vue"),
    children: [
      {
        path: "/main/system",
        component: RouterView,
        meta: { title: "系统管理" },
        children: [
          {
            path: "/main/system/user/user",
            name: "user",
            component: () => import("@/views/main/system/user/user.vue"),
            meta: { title: "员工管理" }
          },
          {
            path: "/main/system/role/role",
            name: "role",
            component: () => import("@/views/main/system/role/role.vue"),
            meta: { title: "仓库管理" }
          },
          // 日志管理
          {
            path: "/main/system/menu/login/login",
            component: RouterView,
            meta: { title: "日志管理" },
            children: [
              // 登录日志
              {
                path: "/main/system/menu/login/login",
                name: "configLogin",
                component: () =>
                  import("@/views/main/system/menu/login/login.vue"),
                meta: { title: "登录日志" }
              },
              // 操作日志
              {
                path: "/main/system/menu/menu",
                name: "configMenu",
                component: () => import("@/views/main/system/menu/menu.vue"),
                meta: { title: "操作日志" }
              }
            ]
          }
        ]
      },
      {
        path: "/main/index/index",
        name: "index",
        component: () => import("@/views/main/index/index.vue"),
        meta: { title: "首页" }
      },
      {
        // 个人中心
        path: "/main/index/usercenter",
        name: "usercenter",
        component: () => import("@/views/main/index/usercenter.vue"),
        meta: { title: "个人中心" }
      },
      // 系统监控
      {
        path: "/main/analysis/overview/overview",
        name: "overview",
        component: () => import("@/views/main/analysis/overview/overview.vue"),
        meta: { title: "系统监控" }
      },
      // 订单管理
      {
        path: "/main/product/category",
        component: RouterView,
        meta: { title: "订单管理" },
        children: [
          {
            path: "/main/product/category",
            name: "category",
            component: () =>
              import("@/views/main/product/category/category.vue"),
            meta: { title: "审核订单" }
          },
          {
            path: "/main/product/categoryList",
            name: "categoryList",
            component: () =>
              import("@/views/main/product/categoryList/categoryList.vue"),
            meta: { title: "订单列表" }
          }
        ]
      },
      // 商品管理
      {
        path: "/main/goods",
        component: RouterView,
        meta: { title: "商品管理" },
        children: [
          // 商品列表
          {
            path: "goodsList",
            name: "goodsList",
            component: () => import("@/views/main/goods/goodsList.vue"),
            meta: { title: "商品列表" }
          },
          // 商品分类
          {
            path: "goodsCategory",
            name: "goodsCategory",
            component: () =>
              import("@/views/main/goods/category/goodsCategory.vue"),
            meta: { title: "商品分类" }
          }
        ]
      }
    ]
  },
  {
    name: "pathMatch",
    path: "/:pathMatch(.*)*",
    component: () => import("@/views/not-found/not-found.vue")
  }
];

const router = createRouter({
  routes,
  history: createWebHashHistory()
});
// 前置路由进行判断
router.beforeEach((to) => {
  const token = LocalCache.getCache("token");
  if (to.path !== "/login") {
    if (!token) {
      return "/login";
    }
  }
});
export default router;
